#include "StdAfx.h"
#include "pgl/shape/PGLGeoEllipse.h"

void CPGLGeoEllipse::SetCenter(double x,double y)
{
	if(m_center.x-x<TOL&&m_center.y-y<TOL)
		return;
	m_center=CPGLPointD(x,y);
	ReDraw();
}
CPGLPointD CPGLGeoEllipse::GetCenter()
{
	return m_center;
}
void CPGLGeoEllipse::SetCenter(CPGLPointD center)
{
	if(m_center.x-center.x<TOL&&m_center.y-center.y<TOL)
		return;
	m_center=center;
	ReDraw();
}
void CPGLGeoEllipse::SetWidth(double width)
{
	if(m_w-width<TOL)
		return;
	m_w=width;
	ReDraw();
}
double CPGLGeoEllipse::GetWidth()
{
	return m_w;
}
void CPGLGeoEllipse::SetHeight(double height)
{
	if(m_h-height<TOL)
		return;
	m_h=height;
	ReDraw();
}
double CPGLGeoEllipse::GetHeight()
{
	return m_h;
}
void CPGLGeoEllipse::SetSize(double width,double height)
{
	if(m_h-height<TOL&&m_w-width<TOL)
		return;
	m_h=height;
	m_w=width;
	ReDraw();
}
CPGLPointD CPGLGeoEllipse::GetSize()
{
	return CPGLPointD(m_w,m_h);
}
void CPGLGeoEllipse::ReDraw()
{
	int npts=360;
	double stepX=2*m_w/npts;
	double startX=m_center.x-abs(m_w);
	std::vector<double>* vx=new std::vector<double>;
	std::vector<double>* vy=new std::vector<double>;
	double y_y02=0;
	double y_y02_b2=0;
	double _y_y02_b2=0;
	double a2_y_y02_b2=0;
	double y=0;
	while(startX<=m_center.x+abs(m_w))
	{
		y_y02=(startX-m_center.x)*(startX-m_center.x);
		y_y02_b2=y_y02/(m_w*m_w);
		_y_y02_b2=1-y_y02_b2;
		a2_y_y02_b2=m_h*m_h*_y_y02_b2;
		y=m_center.x+abs(a2_y_y02_b2);
		vx->push_back(startX);
		vy->push_back(y);
		startX+=stepX;
	}
	startX-=stepX;
	while(startX>=m_center.x-abs(m_w))
	{
		y_y02=(startX-m_center.x)*(startX-m_center.x);
		y_y02_b2=y_y02/(m_w*m_w);
		_y_y02_b2=1-y_y02_b2;
		a2_y_y02_b2=m_h*m_h*_y_y02_b2;
		y=m_center.x-abs(a2_y_y02_b2);
		vx->push_back(startX);
		vy->push_back(y);
		startX-=stepX;
	}
	SetDatas(*vx,*vy);
	delete vx;
	vx=0;
	delete vy;
	vy=0;
}